
using System;
using System.IO;
using System.IO.Compression;
using System.Text;

public static class zipgen
{
    private static void w(string s)
    {
        Console.WriteLine(s);
    }

    private static void w(int x)
    {
        Console.Write(x);
    }

    private static void wc()
    {
        Console.Write(",");
    }

    private static void wb()
    {
        Console.Write(" ");
    }

    private static void w()
    {
        Console.WriteLine();
    }

    private static void ws()
    {
        Console.Write("          ");
    }

    private static void wvs()
    {
        Console.Write("        { ");
    }

    private static string strcat(params string[] ss)
    {
        if(ss == null) return null;
        else
        {
            string r = null;
            for(int i = 0; i < ss.Length; i++)
                r += ss[i];
            return r;
        }
    }

    private static string merge_args(string[] args)
    {
        string r = null;
        for(int i = 1; i < args.Length; i += 2)
            r += args[i] + (i < args.Length - 2 ? " " : "");
        return r;
    }

    private static string strleft(string s, int len)
    {
        if(s.Length < len) return s;
        else return s.Substring(0, len);
    }

    private static string strmid(string s, int start)
    {
        if(s.Length < start) return null;
        else return s.Substring(start);
    }

    private static string cut_lines(string s)
    {
        StringBuilder r = null;
        r = new StringBuilder();
        const int line_len = 64;
        while(!string.IsNullOrEmpty(s))
        {
            string c = strleft(s, line_len);
            s = strmid(s, line_len);
            r.Append("        \"");
            r.Append(c);
            r.Append("\"");
            if(!string.IsNullOrEmpty(s)) r.Append(" +\n");
        }
        return r.ToString();
    }

    public static void Main(string[] args)
    {
        if(args == null || (args.Length & 1) != 1 || args.Length < 3)
            w("zipgen module_name variable_name1 file1 variable_name2 file2 ...");
        else
        {
            w();
            w("\'this file is generated by /osi/service/resource/zipgen/zipgen.exe with");
            w("\'" + merge_args(args));
            w("\'so change /osi/service/resource/gen/gen.cs or resource files instead of this file");
            w();
            w("Imports System.IO");
            w("Imports System.IO.Compression");
            w("Imports osi.root.connector");
            w();
            w("Friend Module " + args[0]);
            for(int i = 1; i < args.Length; i += 2)
            {
                // a long initialize list will crash visual studio 2012 update 4
                w("    Public ReadOnly " + args[i] + "() As Byte");
            }
            w();
            w("    Sub New()");
            for(int i = 1; i < args.Length; i += 2)
            {
                using(MemoryStream ms = new MemoryStream())
                {
                    using(Stream fs = new FileStream(args[i + 1], FileMode.Open, FileAccess.Read),
                                 zs = new GZipStream(ms, CompressionMode.Compress, true))
                    {
                        int j = 0;
                        while((j = fs.ReadByte()) != -1)
                            zs.WriteByte((byte)j);
                    }
                    ms.Capacity = (int)ms.Length;
                    w("        " + args[i] + " = Convert.FromBase64String( _\n" + cut_lines(Convert.ToBase64String(ms.ToArray())) + ")");
                }
                w();
                w("        k_assert(" + args[i] + ".ungzip(" + args[i] + "))");
            }
            w("    End Sub");
            w("End Module");
        }
    }
}

